<?php

require_once 'Core/Content/Filter.php';

class Core_View_Filter_CoreControl extends Core_Content_Filter
{
    /**
     * Attributes for the specific navigation helper
     * @var array
     */
    protected $_attribs = array(
        'type'     => null,
        'id'       => null,
        'required' => null,
        'group'    => null,
        'cols'     => null,
        'rows'     => null,
    );

    /**
     * Attributes that must be booleanised before processing them
     * @var array
     */
    protected $_booleanValues = array('required');

    /**
     * The "Html" tag
     * @var string
     */
    public $tag = 'coreControl';
    protected $_id = '';

    protected function _callback($matches)
    {
        $attribs = $this->getAttributes($matches[0]);

        // make use of booleanise function
        $this->_setAttribs($attribs);
        $attribs = $this->_getAttribs();

        if (is_array($attribs) && is_object($this->page)) {
            $content = $this->page->getContent();
            if (isset($attribs['id']) && !is_null($attribs['id']) && isset($content[$attribs['id']])) {
                // set id to protected variable
                $this->_setId($attribs['id']);
                $controlContent = $content[$this->getId()];
                switch ($attribs['type']) {
                    case 'fckeditor':
                    case 'markitup':
                    case 'tinymce':
                    case 'wymeditor':
                    case 'wysiwyg':
                        $xhtml = '<div id="' . $this->getId() . '_wrapper">' . $controlContent . '</div>';
                        break;
                    case 'text':
                    case 'textarea':
                    default:
                        $xhtml = $controlContent;
                        break;
                    case 'moduleSelector':
                        $xhtml = $this->view->renderModule($this->getId(), $controlContent);
                        break;
                }
                if (isset($attribs['tag']) && !empty($xhtml)) {
                    return '<' . $attribs['tag'] . '>' . $xhtml . '</' . $attribs['tag'] . '>';
                } else {
                    return $xhtml;
                }
            }
        }
        return null;
    }

    public function getId()
    {
        return $this->_id;
    }

    protected function _setId($id)
    {
        if (isset($id) && !empty($id) && $id != '') {
            $this->_id = $id;
        }
        return $this->_id;
    }
}